package com.itheima.controller;

import com.itheima.health.dto.SetmealDTO;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.service.SetmealService;
import com.itheima.health.utils.aliyunoss.AliyunUtils;
import com.itheima.health.utils.redis.RedisUtil;
import com.itheima.health.utils.resources.RedisConstant;
import com.itheima.health.utils.resources.UploadUtils;
import io.swagger.annotations.*;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

@RestController
@Api(tags = "套餐管理开发") // 注解打在类上
public class SetmealController {

    @Reference
    private SetmealService setmealService;

    @Autowired
    private RedisTemplate redisTemplate;


//    @ApiOperation(value = "检查项列表查询",notes = "根据客户端请求，完成检查项所有信息列表查询")//   添加业务方法上
//    @GetMapping("checkGroup/findCheckItemInfoByGroupId/{id}")
//    public Result findCheckItemInfoByGroupId(@PathVariable("id") int id){
//        return   new Result(checkGroupService.findCheckItemInfoByGroupId(id));
//    }

//    @ApiOperation(value = "检查项列表查询", notes = "根据客户端请求，完成分页查询")
//    // 查询所有的检查项信息，Result 响应数据规范 json 格式数据
//    @GetMapping("checkItem/findAll")
//    public Result findAll() {
//        return new Result(checkItemService.list());
//    }

    @ApiOperation(value = "分页查询", notes = "根据客户端请求，完成分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currentPage", value = "当前分页页码", required = true, paramType = "form", dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示记录数", required = true, paramType = "form", dataType = "Integer"),
            @ApiImplicitParam(name = "queryString", value = "查询条件", required = false, paramType = "form", dataType = "String")
    })
    @ApiResponses({
            @ApiResponse(code = 400, message = "请求参数没填好"),
            @ApiResponse(code = 404, message = "请求路径没有或页面跳转路径不对")
    })
    //  上述注解 添加在目标方法上！

    //  分页查询
    @PostMapping("setmeal/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean) {

        return new Result(setmealService.findPage(queryPageBean)); // total + list
    }


    //  异常处理机制：  业务出现异常   抛出 给调用者  ： 调用者捕捉异常  处理
    //  web  全局异常处理机制   统一处理
    @PostMapping("setmeal/add")
    public Result add(@RequestBody SetmealDTO setmealDTO) {
        return new Result(setmealService.saveSetmeal(setmealDTO));  //   业务代码实现
    }

    @PostMapping("setmeal/upload")
    public Result upload(@RequestParam("imgFile") MultipartFile file) {
        try {
            //  MultipartFile file  上传文件对象
            String originalFilename = file.getOriginalFilename();
            String uuidfilename = UploadUtils.generateRandonFileName(originalFilename);
            //  阿里云sdk
            AliyunUtils.uploadMultiPartFileToAliyun(file.getBytes(), uuidfilename);
            //   图片信息 保存  set 集合
            RedisUtil.addToSet(RedisConstant.ALL_SETMEAL_PIC_SET, uuidfilename);  //   图片存储到set集合
            //  图片存储到redis  保存30秒   超过 30秒 redis  自动删除该图片信息
            RedisUtil.set(RedisConstant.SINGLE_PIC + uuidfilename, uuidfilename, 300, TimeUnit.SECONDS);
            return new Result(uuidfilename);
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());
        }
    }
}
